package weaver.interfaces.geely.duss.utils;

import weaver.conn.RecordSet;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.BaseBean;
import weaver.general.TimeUtil;
import weaver.general.Util;

import java.util.Map;
import java.util.Set;
import java.util.UUID;

/**
 * @author dusongsong
 * @date 2024年12月23日 17:58
 */
public class SapUtil extends BaseBean {


    /**
     * 建模权限重构
     *
     * @param userid
     * @param formmodeid
     * @param id
     */
    private void newModeRight(int userid, int formmodeid, int id) {
        ModeRightInfo modeRightInfo = new ModeRightInfo();
        modeRightInfo.setNewRight(true);
        modeRightInfo.editModeDataShare(userid, formmodeid, id);
    }

    /**
     * 将数据插入表单建模
     *
     * @param map
     */
    public void insertModelData(Map<String, Object> map) {
        String tablename = "uf_apilog";
        int userid = 1;
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        //查询该模块表名
        try {
            rs.executeQuery("select a.id from modeinfo a left join workflow_bill b on a.formid=b.id where b.tablename='" + tablename + "'");
            rs.next();
            int modeid = Util.getIntValue(rs.getString("id"));
            String uuid = UUID.randomUUID().toString();
            String insSql = "insert into " + tablename
                    + "(modeuuid,modedatacreater,modedatacreatedate,modedatacreatetime,formmodeid)values" +
                    "('" + uuid + "','" + userid + "','" + TimeUtil.getCurrentDateString() + "','" + TimeUtil.getOnlyCurrentTimeString() + "','" + modeid + "')";
            rs.executeUpdate(insSql);
            rs1.executeQuery("select id from " + tablename + " where modeuuid='" + uuid + "'");
            rs1.next();
            int dataid = Util.getIntValue(rs1.getString("id"));
            if (dataid > 0) {
                //遍历数据 进行update
                String updatesql = "update " + tablename + " set ";
                Set<String> keySet = map.keySet();
                for (String key : keySet) {
                    String value = Util.null2String(map.get(key)).replace("'", "").replace("-", "").replace(" ", "");
                    updatesql += key + "='" + value + "',";
                }
                if (updatesql.endsWith(",")) {
                    updatesql = updatesql.substring(0, updatesql.length() - 1);
                    updatesql += " where id=" + dataid;
                    if (!rs.executeUpdate(updatesql)) {
                        rs1.executeUpdate("delete from " + tablename + " where id=?", dataid);
                    }
                }
                //进行权限重构
                newModeRight(userid, modeid, dataid);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
